package com.pokermind.dse.tcp.protocol;

/**
 * TCP Protocol Constants TCP二进制协议常量定义
 *
 * Protocol Format: +----------+----------+------------------+ | Protocol | Body     | Protobuf Body    | | ID       |
 * Length   |                  | | (2 bytes)| (4 bytes)| (N bytes)        | +----------+----------+------------------+
 *
 * 协议设计原则： 1. 协议号分段管理，便于扩展和维护 2. 请求和响应协议号成对出现 3. 预留充足的协议号空间 4. 支持协议版本升级
 *
 * @author PokerMind Team
 */
public final class ProtocolConstants {

    private ProtocolConstants() {
        // Utility class
    }

    // ===============================================
    // Protocol Header Configuration
    // ===============================================
    public static final int HEADER_SIZE = 6;           // Protocol ID (2) + Body Length (4)
    public static final int PROTOCOL_ID_SIZE = 2;      // 2 bytes for protocol ID
    public static final int BODY_LENGTH_SIZE = 4;      // 4 bytes for body length
    public static final int MAX_BODY_SIZE = 64 * 1024; // 64KB maximum body size

    // ===============================================
    // Hand Management Protocol (2000-2099)
    // ===============================================

    // Hand Management Requests
    public static final short CREATE_HAND_REQ = 2001;
    public static final short RECORD_ACTION_REQ = 2002;
    public static final short TRANSITION_STREET_REQ = 2003;
    public static final short COMPLETE_HAND_REQ = 2004;
    public static final short UPDATE_HERO_CARDS_REQ = 2005;  // ⚠️ Deprecated in V4.1

    // Hand Management Responses  
    public static final short CREATE_HAND_RESP = 2101;
    public static final short RECORD_ACTION_RESP = 2102;
    public static final short TRANSITION_STREET_RESP = 2103;
    public static final short COMPLETE_HAND_RESP = 2104;
    public static final short UPDATE_HERO_CARDS_RESP = 2105;  // ⚠️ Deprecated in V4.1

    // ===============================================
    // AI Analysis Protocol (3000-3099)
    // ===============================================

    // AI Analysis Requests
    public static final short AI_ANALYZE_REQ = 3001;

    // AI Analysis Responses
    public static final short AI_ANALYZE_RESP = 3101;

    // ===============================================
    // System Protocol (9000-9999)
    // ===============================================

    // System Requests
    public static final short HEARTBEAT_REQ = 9001;
    public static final short AUTH_REQ = 9002;
    public static final short SERVER_STATUS_REQ = 9003;

    // System Responses
    public static final short HEARTBEAT_RESP = 9101;
    public static final short AUTH_RESP = 9102;
    public static final short SERVER_STATUS_RESP = 9103;

    // ===============================================
    // Error Protocol (8000-8999)
    // ===============================================
    public static final short ERROR_RESP = 8000;
    public static final short INVALID_PROTOCOL_RESP = 8001;
    public static final short SERVER_ERROR_RESP = 8002;
    public static final short BUSINESS_ERROR_RESP = 8003;

    // ===============================================
    // Protocol Validation and Utilities
    // ===============================================

    /**
     * 获取协议名称（用于日志和调试）
     */
    public static String getProtocolName(short protocolId) {
        switch (protocolId) {
            // Hand Management Requests
            case CREATE_HAND_REQ:
                return "CREATE_HAND_REQ";
            case UPDATE_HERO_CARDS_REQ:
                return "UPDATE_HERO_CARDS_REQ";
            case TRANSITION_STREET_REQ:
                return "TRANSITION_STREET_REQ";
            case RECORD_ACTION_REQ:
                return "RECORD_ACTION_REQ";
            case COMPLETE_HAND_REQ:
                return "COMPLETE_HAND_REQ";

            // Hand Management Responses
            case CREATE_HAND_RESP:
                return "CREATE_HAND_RESP";
            case UPDATE_HERO_CARDS_RESP:
                return "UPDATE_HERO_CARDS_RESP";
            case TRANSITION_STREET_RESP:
                return "TRANSITION_STREET_RESP";
            case RECORD_ACTION_RESP:
                return "RECORD_ACTION_RESP";
            case COMPLETE_HAND_RESP:
                return "COMPLETE_HAND_RESP";

            // AI Analysis
            case AI_ANALYZE_REQ:
                return "AI_ANALYZE_REQ";
            case AI_ANALYZE_RESP:
                return "AI_ANALYZE_RESP";

            // System
            case HEARTBEAT_REQ:
                return "HEARTBEAT_REQ";
            case HEARTBEAT_RESP:
                return "HEARTBEAT_RESP";
            case AUTH_REQ:
                return "AUTH_REQ";
            case AUTH_RESP:
                return "AUTH_RESP";
            case SERVER_STATUS_REQ:
                return "SERVER_STATUS_REQ";
            case SERVER_STATUS_RESP:
                return "SERVER_STATUS_RESP";

            // Error
            case ERROR_RESP:
                return "ERROR_RESP";
            case INVALID_PROTOCOL_RESP:
                return "INVALID_PROTOCOL_RESP";
            case SERVER_ERROR_RESP:
                return "SERVER_ERROR_RESP";
            case BUSINESS_ERROR_RESP:
                return "BUSINESS_ERROR_RESP";

            default:
                return "UNKNOWN_PROTOCOL(" + protocolId + ")";
        }
    }

    /**
     * 检查协议ID是否有效
     */
    public static boolean isValidProtocol(short protocolId) {
        return isHandManagementProtocol(protocolId) || isAIAnalysisProtocol(protocolId) || isSystemProtocol(
            protocolId) || isErrorProtocol(protocolId);
    }

    /**
     * 检查是否为手牌管理协议（包含请求和响应）
     */
    public static boolean isHandManagementProtocol(short protocolId) {
        return (protocolId >= 2000 && protocolId <= 2099) ||  // Requests: 2000-2099
               (protocolId >= 2100 && protocolId <= 2199);    // Responses: 2100-2199
    }

    /**
     * 检查是否为AI分析协议（包含请求和响应）
     */
    public static boolean isAIAnalysisProtocol(short protocolId) {
        return (protocolId >= 3000 && protocolId <= 3099) ||  // Requests: 3000-3099
               (protocolId >= 3100 && protocolId <= 3199);    // Responses: 3100-3199
    }

    /**
     * 检查是否为系统协议
     */
    public static boolean isSystemProtocol(short protocolId) {
        return (protocolId >= 9000 && protocolId <= 9999);
    }

    /**
     * 检查是否为错误协议
     */
    public static boolean isErrorProtocol(short protocolId) {
        return (protocolId >= 8000 && protocolId <= 8999);
    }

    /**
     * 检查是否为请求协议
     */
    public static boolean isRequest(short protocolId) {
        return (protocolId >= 2000 && protocolId <= 2099) ||  // Hand Management
            (protocolId >= 3000 && protocolId <= 3099) ||  // AI Analysis
            (protocolId >= 9000 && protocolId <= 9999);    // System
    }

    /**
     * 检查是否为响应协议
     */
    public static boolean isResponse(short protocolId) {
        return (protocolId >= 2100 && protocolId <= 2199) ||  // Hand Management Response
            (protocolId >= 3100 && protocolId <= 3199) ||  // AI Analysis Response
            (protocolId >= 8000 && protocolId <= 8999) ||  // Error Response
            (protocolId >= 9100 && protocolId <= 9199);    // System Response
    }

    /**
     * 获取对应的响应协议ID
     */
    public static short getResponseProtocol(short requestProtocol) {
        switch (requestProtocol) {
            // Hand Management
            case CREATE_HAND_REQ:
                return CREATE_HAND_RESP;
            case UPDATE_HERO_CARDS_REQ:
                return UPDATE_HERO_CARDS_RESP;
            case TRANSITION_STREET_REQ:
                return TRANSITION_STREET_RESP;
            case RECORD_ACTION_REQ:
                return RECORD_ACTION_RESP;
            case COMPLETE_HAND_REQ:
                return COMPLETE_HAND_RESP;

            // AI Analysis
            case AI_ANALYZE_REQ:
                return AI_ANALYZE_RESP;

            // System
            case HEARTBEAT_REQ:
                return HEARTBEAT_RESP;
            case AUTH_REQ:
                return AUTH_RESP;
            case SERVER_STATUS_REQ:
                return SERVER_STATUS_RESP;

            default:
                return ERROR_RESP;
        }
    }

    /**
     * 获取协议类型描述
     */
    public static String getProtocolType(short protocolId) {
        if (isHandManagementProtocol(protocolId)) {
            return "HAND_MANAGEMENT";
        } else if (isAIAnalysisProtocol(protocolId)) {
            return "AI_ANALYSIS";
        } else if (isSystemProtocol(protocolId)) {
            return "SYSTEM";
        } else if (isErrorProtocol(protocolId)) {
            return "ERROR";
        } else {
            return "UNKNOWN";
        }
    }
}
// V4.10: Protocol ID updated to match documentation
// Hand Management: 2000-2099 (was 1000-1999)
// AI Analysis: 3000-3099 (was 2000-2999)
